<?php

namespace app\common\model\coupon;

use think\Model;
use traits\model\SoftDelete;

class Coupon extends Model
{

    use SoftDelete;



    // 表名
    protected $name = 'coupon';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'state_text',
        'type_text',
        'image_str',
        'goods_type_text'
    ];


    public function getImageStrAttr($value, $data)
    {
        return $data['image'] ? cdnurl($data['image'], true) : '';
    }

    public function getStateList()
    {
        return ['0' => __('State 0'), '1' => __('State 1')];
    }

    public function getTypeList()
    {
        return ['1' => __('Type 1')];
    }

    public function getGoodsTypeList()
    {
        return ['0' => __('Goods_type 0'), '1' => __('Goods_type 1')];
    }


    public function getStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['state']) ? $data['state'] : '');
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getGoodsTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['goods_type']) ? $data['goods_type'] : '');
        $list = $this->getGoodsTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public static function getCoupon($uid)
    {
        $log = Log::get(['user_id' => $uid, 'type' => 3]);
        if (!$log && $uid) {
            return  self::get(['type' => 3]);
        }
        return [];
    }
 
}
